<template>
  <div class="container">
    <el-row>
      <el-form :model="formInline">
        <el-col :span="8" align="left">
          <el-form-item :label="$t('m.in.ordernumber')" :label-width="formLabelWidth">
            <el-input maxlength="30" show-word-limit v-model="formInline.ordercode"></el-input>
          </el-form-item>
          <el-form-item label="SKU" :label-width="formLabelWidth">
            <el-input maxlength="20" show-word-limit v-model="formInline.productspeccode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" align="left">
          <el-form-item :label="$t('m.out.storeordercode')" :label-width="formLabelWidth">
            <el-input maxlength="20" show-word-limit v-model="formInline.storeordercode" placeholder></el-input>
          </el-form-item>
          <el-form-item label="SupplierCode" :label-width="formLabelWidth">
            <el-input maxlength="20" show-word-limit v-model="formInline.suppliercode" placeholder></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" align="left">
          <el-form-item :label="$t('m.out.ordertype')" :label-width="formLabelWidth">
            <el-select v-model="formInline.ordertype" :placeholder="$t('m.choose')">
              <el-option v-for="item in ordertype" :key="item.id" :label="item.label" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('m.tasknumber')" :label-width="formLabelWidth">
            <el-input maxlength="20" show-word-limit v-model="formInline.tasknumber" placeholder></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" align="right">
          <el-form-item>
            <el-button @click="search" v-if="hasPermission('search')">{{$t('m.query')}}</el-button>
            <el-button type="primary" @click="doReview" v-if="hasPermission('review')">{{$t('m.review.review')}}</el-button>
            <!-- <el-button type @click="print">{{$t('m.print')}}</el-button> -->
            <!-- <el-button type @click="editOrder">{{$t('m.edit')}}</el-button> -->
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div class="ellipse"></div>
    <el-table ref="table" stripe empty-text fits class="table-wrap single-select-table" :data="tableData" :row-style="tableRowStyle" :row-class-name="tableRowClassName" @row-click="rowClick" @selection-change="handleSingleSelectionChange" v-loading="tableLoading">
      <el-table-column align="center" type="selection" width="55"></el-table-column>
      <el-table-column align="center" type="index" :label="$t('m.index')" width="50"></el-table-column>
      <el-table-column align="center" prop="reviewcode" :label="$t('m.review.reviewcode')" show-overflow-tooltip>
        <template slot-scope="scope">
          <template v-if="hasPermission('table-reviewCode')">
            <el-button type="text" @click.stop="reviewResultOpen(scope.row)">{{scope.row.reviewcode}}</el-button>
          </template>
          <template v-else>{{scope.row.reviewcode}}</template>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="ordercode" :label="$t('m.in.ordernumber')" show-overflow-tooltip>
        <template slot-scope="scope">
          <template v-if="hasPermission('table-orderCode')">
            <el-button type="text" @click.stop="$router.push({ path: 'order/orderInfo', query: {orderid:scope.row.orderid}})">{{scope.row.ordercode}}</el-button>
          </template>
          <template v-else>{{scope.row.ordercode}}</template>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="storeordercode" :label="$t('m.out.storeordercode')"></el-table-column>
      <el-table-column width="100" :label="$t('m.state')" align="center" prop="statetext">
        <template slot-scope="scope">
          <span v-if="scope.row.state==1">{{$t('m.review.state1')}}</span>
          <span v-if="scope.row.state==2">{{$t('m.review.state2')}}</span>
          <span v-if="scope.row.state==3">{{$t('m.review.state3')}}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('m.out.ordertype')" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.ordertype==1">{{$t('m.out.ordertype1')}}</span>
          <span v-if="scope.row.ordertype==2">{{$t('m.out.ordertype2')}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="totalnumber" :label="$t('m.out.totalnumber')"></el-table-column>
      <el-table-column align="center" prop="consignee" :label="$t('m.out.consignee')"></el-table-column>
      <el-table-column align="center" prop="contact" :label="$t('m.out.contact')"></el-table-column>
      <el-table-column align="center" prop="provincename" :label="$t('m.out.provincename')"></el-table-column>
      <el-table-column align="center" prop="cityname" :label="$t('m.out.cityname')"></el-table-column>
      <el-table-column align="center" prop="regionname" :label="$t('m.out.regionname')"></el-table-column>
      <el-table-column align="center" prop="shippingaddress" :label="$t('m.out.address')"></el-table-column>
    </el-table>

    <el-pagination background layout="sizes, prev, pager, next, jumper" :page-sizes="pageSizeList" :page-size="rowSize" :current-page="page" :total="totalRows" @current-change="currentChangeInitFn" @size-change="handleSizeChangeInitFn"></el-pagination>

    <!-- 复核结果 dialog 开始 -->
    <el-dialog width="1200px" :title="$t('m.review.reviewtitle')" :visible.sync="dialogFormVisible" @close="close">
      <div v-loading="popLoading">
        <el-row :gutter="30" class="waveHead">
          <el-col :span="6" align="left" :offset="2">
            <b>{{$t('m.review.reviewcode')}}:</b>
            <span>{{reviewData.reviewcode}}</span>
          </el-col>
          <el-col :span="6" align="left">
            <b>{{$t('m.in.ordernumber')}}:</b>
            <span>{{reviewData.ordercode}}</span>
          </el-col>
          <el-col :span="10" align="left">
            <b>{{$t('m.out.storeordercode')}}:</b>
            <span>{{reviewData.storeordercode}}</span>
          </el-col>
        </el-row>
        <el-row class="waveHead" :gutter="30" style="margin-bottom:30px">
          <el-col :span="6" align="left" :offset="2">
            <b>{{$t('m.state')}}:</b>
            <span v-if="reviewData.state==1">{{$t('m.review.state1')}}</span>
            <span v-if="reviewData.state==2">{{$t('m.review.state2')}}</span>
            <span v-if="reviewData.state==3">{{$t('m.review.state3')}}</span>
          </el-col>
          <el-col :span="6" align="left">
            <b>{{$t('m.review.reviewusers')}}:</b>
            <span>{{reviewData.reviewusers}}</span>
          </el-col>
          <el-col :span="10" align="left">
            <b>{{$t('m.review.reviewdate')}}:</b>
            <span>{{reviewData.reviewdate}}</span>
          </el-col>
        </el-row>
        <el-table ref="resultTable" :data="reviewData.reviewDetailOutputs" stripe empty-text fits>
          <el-table-column align="center" type="index" :label="$t('m.index')" width="50"></el-table-column>
          <el-table-column align="center" width="150" :label="$t('m.tasknumber')" prop="tasknumber"></el-table-column>
          <el-table-column align="center" label="SKU" prop="productspeccode"></el-table-column>
          <el-table-column align="center" :label="$t('m.state')" prop="state">
            <template slot-scope="scope">
              <span v-if="scope.row.state==1">{{$t('m.review.state1')}}</span>
              <span v-if="scope.row.state==2">{{$t('m.review.state2')}}</span>
              <span v-if="scope.row.state==3">{{$t('m.review.state3')}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('m.in.productname')" prop="productname"></el-table-column>
          <el-table-column align="center" :label="$t('m.in.productspec')" prop="productspec"></el-table-column>
          <el-table-column align="center" :label="$t('m.out.quantity')" prop="quantity"></el-table-column>
          <el-table-column align="center" :label="$t('m.review.cartons')" prop="cartons"></el-table-column>
          <el-table-column align="center" :label="$t('m.review.bulkitems')" prop="bulkitems"></el-table-column>
        </el-table>
        <div class="btns">
          <el-button size="mini" @click="close">{{$t('m.close')}}</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 复核结果 dialog 结束 -->

    <!-- 复核 dialog 开始 -->
    <el-dialog :close-on-click-modal="false" width="1200px" :title="$t('m.review.reviewtitle')" :visible.sync="dialogFormVisible1" @close="closeReview">
      <div v-loading="popLoading">
        <el-row :gutter="40">
          <el-col :span="16">
            <el-form :model="reviewForm" ref="reviewForm">
              <el-row>
                <el-col :span="24">
                  <el-form-item :rules="[
      { required: true, message: $t('m.enter')+$t('m.tasknumber'), trigger: 'blur' }
    ]" :label="$t('m.tasknumber')" :label-width="formLabelWidth" prop="reviewpackagingdetail.tasknumber">
                    <el-input maxlength="20" show-word-limit @input="func" @blur="blur&&getInfoBlur()" @keyup.enter.native="getInfoEnter" v-model.trim="reviewForm.reviewpackagingdetail.tasknumber" placeholder></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item :rules="[
      { required: true, message: $t('m.enter')+'SKU', trigger: 'blur' }
    ]" label="SKU" :label-width="formLabelWidth" prop="reviewpackagingdetail.productspeccode">
                    <el-input maxlength="20" show-word-limit @input="func2" @blur="blur2&&getSkuInfoBlur()" @keyup.enter.native="getSkuInfoEnter" v-model.trim="reviewForm.reviewpackagingdetail.productspeccode" placeholder></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    :rules="[
      { required: true, message: $t('m.enter')+$t('m.review.packingrate'), trigger: 'blur' },
      { type: 'number',min:0, message: $t('m.enter')+$t('m.check.number')}
    ]"
                    :label="$t('m.review.packingrate')"
                    :label-width="formLabelWidth"
                    prop="packingrate"
                  >
                    <el-input maxlength="4" show-word-limit @input="doMath" @change="inputAlert" v-model.number="reviewForm.packingrate" placeholder></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    :rules="[{ required: true, message: $t('m.enter')+$t('m.review.cartons'), trigger: 'blur' },{ type: 'number',min:0, message: $t('m.enter')+$t('m.check.number')}]"
                    :label="$t('m.review.cartons')"
                    :label-width="formLabelWidth"
                    prop="reviewpackagingdetail.cartons"
                  >
                    <el-input maxlength="4" show-word-limit @change="inputAlert" v-model.number="reviewForm.reviewpackagingdetail.cartons" placeholder></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item
                    :rules="[
      { required: true, message: $t('m.enter')+$t('m.review.bulkitems'), trigger: 'blur' },
      { type: 'number',min:0, message: $t('m.enter')+$t('m.check.number')}
    ]"
                    :label="$t('m.review.bulkitems')"
                    :label-width="formLabelWidth"
                    prop="reviewpackagingdetail.bulkitems"
                  >
                    <el-input maxlength="4" show-word-limit @change="inputAlert" v-model.number="reviewForm.reviewpackagingdetail.bulkitems" placeholder></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div class="btns">
              <el-button type="primary" @click="saveTask">{{$t('m.save')}}</el-button>
            </div>
          </el-col>
          <el-col :span="8" align="left">
            <div>
              <img v-if="picLink==''" width="90%" src="@/assets/defalut.png" alt />
              <img v-else width="90%" :src="picLink" alt />
              <p style="margin-top:10px;"></p>
            </div>
          </el-col>
        </el-row>
        <el-table ref="singleTable" stripe :data="reviewForm.reviewpackagingdetails" highlight-current-row @current-change="handleCurrentChange" :row-class-name="rowClassName">
          <el-table-column align="center" type="index" :label="$t('m.index')" width="50"></el-table-column>
          <el-table-column align="center" width="150" :label="$t('m.tasknumber')" prop="tasknumber"></el-table-column>
          <el-table-column align="center" label="SKU" prop="productspeccode"></el-table-column>
          <el-table-column align="center" :label="$t('m.state')" prop="state">
            <template slot-scope="scope">
              <span v-if="scope.row.state==1">{{$t('m.review.state1')}}</span>
              <span v-if="scope.row.state==2">{{$t('m.review.state2')}}</span>
              <span v-if="scope.row.state==3">{{$t('m.review.state3')}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('m.in.productname')" prop="productname"></el-table-column>
          <el-table-column align="center" :label="$t('m.in.productspec')" prop="productspec"></el-table-column>
          <el-table-column align="center" :label="$t('m.out.quantity')" prop="quantity"></el-table-column>
          <el-table-column align="center" :label="$t('m.review.cartons')" prop="cartons"></el-table-column>
          <el-table-column align="center" :label="$t('m.review.bulkitems')" prop="bulkitems"></el-table-column>
        </el-table>
        <div class="btns">
          <el-button size="mini" @click="confirm">{{$t('m.confirm')}}</el-button>
          <el-button size="mini" @click="closeReview">{{$t('m.close')}}</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 复核 dialog 结束 -->

    <el-dialog :close-on-click-modal="false" width="600px" :label="$t('m.review.printexpress')" :visible.sync="dialogFormVisible3" @close="closeBox">
      <el-form :model="boxForm" ref="boxForm" :rules="ruleBoxForm">
        <el-form-item v-for="(item,index) in boxForm.packageinfo" :key="index" :label="item.packagename" :label-width="formLabelWidth" :prop="'packageinfo.'+index+'.packagenumber'" :rules="ruleBoxForm.packagenumber">
          <el-input maxlength="10" show-word-limit v-model="item.packagenumber" placeholder></el-input>
          <p align="left">{{item.packagesize}}</p>
        </el-form-item>
      </el-form>
      <div class="btns">
        <el-button size="mini" @click="boxConfirm">{{$t('m.confirm')}}</el-button>
        <el-button size="mini" @click="closeBox">{{$t('m.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import common from '@/utils/common'
import { getLodop } from '@/utils/LodopFuncs'
export default {
  name:'ReviewPackaging',
  data(){
    let onlyNumber = (rule, value, callback) => {
      if (!/^\+?[0-9]\d*$/.test(value) && value){
        return callback(new Error(this.$t('m.onlynumber')))
      }
      callback()
    }
    return {
      ruleBoxForm:{
        packagenumber:[
          { validator:onlyNumber, trigger:'blur' }
        ]
      },
      bulkitems:'',
      printContent:'',
      blur:true, //判断是否触发blur事件
      blur2:true,
      popLoading:false,
      currentRow:null,
      formInline:{
        ordercode:'',
        ordertype:'',
        shippingaddress:'',
        shopid:'',
        productspeccode:'',
        stateid:null,
        storeordercode:'',
        warehouseid:null,
        suppliercode:null
      },

      tableData:[],
      dialogFormVisible:false,
      dialogFormVisible1:false,
      dialogFormVisible2:false,
      dialogFormVisible3:false,
      formLabelWidth:'140px',
      reviewData:{},
      reviewForm:{
        reviewpackagingdetail:{
          bulkitems:0,
          cartons:0,
          createid:null,
          creator:'',
          detailid:null,
          productid:null,
          productname:'',
          productspec:'',
          productspeccode:'',
          quantity:null,
          reviewid:null,
          suppliercode:'',
          tasknumber:''
        },
        packingrate:0,
        scanBarcode:null,
        reviewpackagingdetails:[]
      },
      reviewDetailOutputs:[],
      reviewid:null,

      boxForm:{
        packageinfo:[]
      },
      picLink:''
    }
  },
  computed:{
    states(){
      return [
        {
          id:null,
          label:this.$t('m.all')
        },
        {
          id:1,
          label:this.$t('m.quality.state1')
        },
        {
          id:2,
          label:this.$t('m.out.state2')
        },
        {
          id:3,
          label:this.$t('m.out.state3')
        },
        {
          id:4,
          label:this.$t('m.review.state1')
        },
        {
          id:5,
          label:this.$t('m.out.state5')
        }
      ]
    },
    ordertype(){
      return [
        {
          id:null,
          label:this.$t('m.all')
        },
        {
          id:1,
          label:this.$t('m.out.ordertype1')
        },
        {
          id:2,
          label:this.$t('m.out.ordertype2')
        }
      ]
    },
    pickingtype(){
      return [
        {
          id:1,
          label:this.$t('m.out.pickingtype1')
        },
        {
          id:2,
          label:this.$t('m.out.pickingtype2')
        }
      ]
    }
  },
  methods:{
    init(){
      this.tableLoading = true
      this.$axios({
        method:'get',
        url:'/reviewpackaging/page',
        params:{
          page:this.page,
          rows:this.rowSize,
          ordercode:this.formInline.ordercode,
          ordertype:this.formInline.ordertype,
          productspeccode:this.formInline.productspeccode,
          storeordercode:this.formInline.storeordercode,
          suppliercode:this.formInline.suppliercode,
          tasknumber:this.formInline.tasknumber
        }
      }).then(({ data }) => {
        for (let i = 0;i < data.records.length;i++){
          data.records[
            i
          ].receivingdate = common.timestampToTime(
            data.records[i].receivingdate
          )
        }

        this.tableData = data.records
        this.totalRows = data.total
        setTimeout(()=>this.tableLoading=false,500)
      })
    },
    search(){
      this.page = 1
      this.init()
    },
    close(){
      // this.$refs[formName].resetFields();
      this.dialogFormVisible = false
      this.reviewData = {}
      this.init()
    },
    closeReview(){
      this.dialogFormVisible1 = false
      this.picLink = ''
      this.$refs['reviewForm'].resetFields()
      this.init()
    },
    reviewResultOpen(row){
      this.reviewid = row.reviewid
      this.dialogFormVisible = true
      this.reviewResult(row)
    },
    doReview(){
      this.reviewForm.reviewpackagingdetails = []
      this.dialogFormVisible1 = true
      if (this.tableRowSelected.length){
        this.reviewid = this.tableRowSelected[0].reviewid
        this.reviewResult(this.tableRowSelected[0])
      }
      this.getpackageinfo()
    },
    reviewResult(){
      this.reviewForm.reviewpackagingdetails = []
      this.popLoading = true
      this.$axios({
        method:'get',
        url:`/reviewpackaging/getreviewwithdetails/${this.reviewid}`
      }).then(({ data }) => {
        data.reviewdate = common.timestampToTime(
          data.reviewdate
        )
        this.reviewData = data
        if (this.tableRowSelected.length){
          this.reviewForm.reviewpackagingdetails =
            data.reviewDetailOutputs
        }
        this.reviewid = data.reviewid
        this.popLoading = false
      })
    },
    func(){
      //通过input事件改变blur的值，避免在enter之后blur事件不能被触发
      this.blur = true
    },
    getInfoEnter(){
      this.blur = false
      this.getInfo()
    },
    getInfoBlur(){
      this.getInfo()
    },
    getInfo(){
      let that = this
      that.picLink = ''
      if (that.reviewForm.reviewpackagingdetail.tasknumber){
        that
          .$axios({
            method:'get',
            url:`/reviewpackaging/getdetailbytasknumber/${that.reviewForm.reviewpackagingdetail.tasknumber}`
          })
          .then(({ data }) => {
            that.scanBarcode = data.scanBarcode
            that.reviewid = data.reviewpackagingdetail.reviewid
            if (data.reviewpackagingdetail.state === 2){
              that.$message({
                showClose:true,
                message:that.$t('m.review.state2'),
                type:'warning',
                duration:1000
              })
            } else {
              if (that.scanBarcode === 0){
                that.reviewForm = data
                that.getPicture()
              } else {
                that.$message(that.$t('m.review.scan'))
                that.reviewForm = data
                that.reviewForm.reviewpackagingdetail.cartons = 0
                that.reviewForm.reviewpackagingdetail.bulkitems = 0
                that.reviewForm.reviewpackagingdetail.productspeccode = ''
              }
            }
            that.popLoading = false
          })
      }
    },
    func2(){
      //通过input事件改变blur的值，避免在enter之后blur事件不能被触发
      this.blur2 = true
    },
    getSkuInfoEnter(){
      this.blur2 = false
      this.getSkuInfo()
    },
    getSkuInfoBlur(){
      this.getSkuInfo()
    },
    getSkuInfo(){
      let that = this
      if (that.reviewForm.reviewpackagingdetail.productspeccode){
        that
          .$axios({
            method:'get',
            url:`/reviewpackaging/getdetailbytasknumberandsku`,
            params:{
              tasknumber:that.reviewForm.reviewpackagingdetail.tasknumber,
              sku:that.reviewForm.reviewpackagingdetail.productspeccode
            }
          })
          .then(({ data }) => {
            if (that.scanBarcode === 1){
              that.reviewForm.reviewpackagingdetail.bulkitems++
            } else {
              that.reviewForm = data
            }

            if (
              that.reviewForm.reviewpackagingdetail.bulkitems ===
              that.reviewForm.packingrate
            ){
              that.reviewForm.reviewpackagingdetail.cartons++
              that.reviewForm.reviewpackagingdetail.bulkitems = 0
            }
            that.getPicture()
            that.popLoading = false
          })
      }
    },
    doMath(){
      let that = this
      if (
        that.reviewForm.packingrate >= 0 &&
        that.reviewForm.reviewpackagingdetail.cartons >= 0 &&
        that.reviewForm.reviewpackagingdetail.bulkitems >= 0
      ){
        //箱数
        that.reviewForm.reviewpackagingdetail.cartons = parseInt(
          that.reviewForm.reviewpackagingdetail.quantity /
            that.reviewForm.packingrate
        )
        //散装
        that.reviewForm.reviewpackagingdetail.bulkitems =
          that.reviewForm.reviewpackagingdetail.quantity %
          that.reviewForm.packingrate
      }
      // if (that.scanBarcode == 1) {
      //   that.reviewForm.reviewpackagingdetail.cartons = 0;
      //   that.reviewForm.reviewpackagingdetail.bulkitems = 0;
      // } else {
      //   //箱数
      //   that.reviewForm.reviewpackagingdetail.cartons = parseInt(
      //     that.reviewForm.reviewpackagingdetail.quantity /
      //       that.reviewForm.packingrate
      //   );
      //   //散装
      //   that.reviewForm.reviewpackagingdetail.bulkitems =
      //     that.reviewForm.reviewpackagingdetail.quantity %
      //     that.reviewForm.packingrate;
      // }
    },
    rowClassName({ row }){
      let that = this
      if (row.tasknumber === that.reviewForm.reviewpackagingdetail.tasknumber){
        return 'warning-row'
      }
      //通过每行的state值判断，如果是2则已经完成，变绿色
      if (row.state === 2){
        return 'success-row'
      }
      return ''
    },
    inputAlert(){
      let that = this
      if (
        parseInt(that.reviewForm.reviewpackagingdetail.cartons) *
        parseInt(that.reviewForm.packingrate) +
        parseInt(that.reviewForm.reviewpackagingdetail.bulkitems) !==
        parseInt(that.reviewForm.reviewpackagingdetail.quantity)
      ){
        that.$message({
          showClose:true,
          message:that.$t('m.review.count'),
          type:'warning',
          duration:1000
        })
      }
    },
    //保存一条任务
    saveTask(){
      // /reviewpackaging/updatedetail
      let that = this
      that.$refs['reviewForm'].validate(valid => {
        if (valid){
          if (
            parseInt(that.reviewForm.reviewpackagingdetail.cartons) *
            parseInt(that.reviewForm.packingrate) +
            parseInt(that.reviewForm.reviewpackagingdetail.bulkitems) ===
            parseInt(that.reviewForm.reviewpackagingdetail.quantity)
          ){
            let obj = {
              bulkitems:that.reviewForm.reviewpackagingdetail.bulkitems,
              cartons:that.reviewForm.reviewpackagingdetail.cartons,
              detailid:that.reviewForm.reviewpackagingdetail.detailid,
              packagerate:that.reviewForm.packingrate
            }
            that
              .$axios({
                method:'put',
                url:'/reviewpackaging/updatedetail',
                data:obj
              })
              .then(() => {
                that.$message({
                  showClose:true,
                  message:that.$t('m.quality.success'),
                  type:'success',
                  duration:1000
                })
                let tasknumber =
                  that.reviewForm.reviewpackagingdetail.tasknumber
                that.$refs['reviewForm'].resetFields()
                that
                  .$axios({
                    method:'get',
                    url:`/reviewpackaging/getdetailbytasknumber/${tasknumber}`
                  })
                  .then(({ data }) => {
                    that.reviewid =
                      data.reviewpackagingdetail.reviewid
                    that.reviewForm.reviewpackagingdetails =
                      data.reviewpackagingdetails
                    tasknumber = ''
                  })
              })
          } else {
            that.$message({
              showClose:true,
              message:that.$t('m.review.count'),
              type:'warning',
              duration:1000
            })
          }
        }
      })
    },
    getPicture(){
      let that = this
      if (that.reviewForm.reviewpackagingdetail.productspeccode){
        that.picLink = ''
        that
          .$axios({
            method:'get',
            url:'/sku/picture',
            params:{
              sku:that.reviewForm.reviewpackagingdetail.productspeccode
            }
          })
          .then(({ data }) => {
            let arr=JSON.parse(data)
            if(arr[0].FilePath){
              return (that.picLink = window._config_.filepath + arr[0].FilePath)
            }
          })
      }
    },
    setCurrent(row){
      this.$refs.singleTable.setCurrentRow(row)
    },
    handleCurrentChange(val){
      this.currentRow = val
    },
    confirm(){
      let that = this
      for (let i = 0;i < that.reviewForm.reviewpackagingdetails.length;i++){
        if (
          that.reviewForm.reviewpackagingdetails.findIndex(
            target => target.state === 1
          ) === -1
        ){
          this.dialogFormVisible3 = true
        } else {
          return that.$message({
            message:that.$t('m.quality.allreview'),
            type:'warning'
          })
        }
      }
    },
    getpackageinfo(){
      this.$axios({
        method:'get',
        url:'/reviewpackaging/getpackageinfo'
      }).then(({ data }) => {
        this.boxForm.packageinfo = data
      })
    },
    print(){
      let LODOP = getLodop()
      let obj = {
        ids:[Number(this.reviewid)],
        templateName:'packing'
      }
      this.$axios({
        method:'post',
        url:'/printtemplate/query',
        data:obj
      }).then(({ data }) => {
        this.printContent = data
        LODOP.SET_PRINT_PAGESIZE(1, '100mm', '73mm', '')
        LODOP.PREVIEW()
        eval(this.printContent)
        LODOP.SET_PRINT_PAGESIZE(1, '100mm', '73mm', '')
        LODOP.PREVIEW()
        this.page = 1
        this.init()
      })
    },
    boxConfirm(){
      // that.reviewForm.reviewpackagingdetails
      let that = this
      let detail = that.reviewForm.reviewpackagingdetails
      that.$refs['boxForm'].validate(valid => {
        if (valid){
          let arr = []
          that.boxForm.packageinfo.forEach((v, i) => {
            arr.push({
              packagename:v.packagename,
              packagenumber:that.boxForm.packageinfo[i].packagenumber
            })
          })

          let items = 0 //判断此单中是否存在散件
          detail.forEach(e => {
            items += Number(e.bulkitems)
          })
          if (items > 0){
            //如果存在散件，则必须输入大小包信息
            let count = 0 //判断输入得大小包是否为0/空
            arr.forEach(e => {
              count += Number(e.packagenumber)
            })
            if (count < 1){
              return that.$message(
                that.$t('m.enter') + that.$t('m.review.info')
              )
            } else {
              that
                .$axios({
                  method:'put',
                  url:`/reviewpackaging/handelreviewpackaging/${that.reviewid}`,
                  data:arr
                })
                .then(() => {
                  that.$message({
                    showClose:true,
                    message:that.$t('m.quality.success'),
                    type:'success',
                    duration:1000
                  })
                  that.dialogFormVisible3 = false
                  that.dialogFormVisible1 = false
                  that.print()
                })
            }
          } else {
            that
              .$axios({
                method:'put',
                url:`/reviewpackaging/handelreviewpackaging/${that.reviewid}`,
                data:arr
              })
              .then(() => {
                that.$message({
                  showClose:true,
                  message:that.$t('m.quality.success'),
                  type:'success',
                  duration:1000
                })
                that.dialogFormVisible3 = false
                that.dialogFormVisible1 = false
                that.print()
              })
          }
        }
      })
    },
    box(){

    },
    closeBox(){
      let that = this
      that.dialogFormVisible3 = false
      that.boxForm.packageinfo.forEach(v => {
        v.packagenumber = 0
      })
    }
  },
  activated(){
    this.init()
  },
  beforeRouteLeave(to, form, next){
    this.dialogFormVisible=false
    this.dialogFormVisible1=false
    this.dialogFormVisible2=false
    this.dialogFormVisible3=false
    next()
  }
}
</script>
<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>